﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Collections.ObjectModel;
using System.Security.Cryptography.X509Certificates;
using DCI6.Client.API;

namespace DCIAPIRMSampleCode.sed_ctv {
    public class DdsManager {
        private static DDSConfiguration _ddsConfiguration;
        public static DDSConfiguration DdsConfiguration {
            get {
                return _ddsConfiguration;
            }
            set {
                if (_ddsConfiguration != null && value != _ddsConfiguration) {
                    _ddsConfiguration = value;
                }
            }
        }



        private ObservableCollection<Advertisement> _advertisementsCollection = new ObservableCollection<Advertisement>();
        
        public IEnumerable<Advertisement> Advertisements { get { return _advertisementsCollection; } }

        public DdsManager(DDSConfiguration config) {
            _ddsConfiguration = config;
        }

        public DdsManager(string pathToConfig) {
            if (System.IO.File.Exists(pathToConfig)) {
                _ddsConfiguration.FromXml(System.IO.File.ReadAllText("configurations.xml"));
            } else {
                _ddsConfiguration.ConnectionID = "TestRegion";
                _ddsConfiguration.DdsContext = "/DDS/services/DDS";
                _ddsConfiguration.DdsHost = "10.12.81.204";
                _ddsConfiguration.DdsPassword = "fsadmin";
                _ddsConfiguration.DdsPort = "7443";
                _ddsConfiguration.DdsProtocol = "https";
                _ddsConfiguration.DdsUser = "ddsuser";
                _ddsConfiguration.DdsSubscriberHost = "172.16.200.62";
                _ddsConfiguration.UseTS3Security = false;
                _ddsConfiguration.WSDL_TIMEOUT = 5000;
                _ddsConfiguration.SOCKET_TIMEOUT = 5000;
                // config.ItemDataSchemaVersioning = DDSConfiguration.ItemDataSchemaVersion.SOFTWARE_BLOCK_11_12;
                _ddsConfiguration.ItemDataSchemaVersioning = DDSConfiguration.ItemDataSchemaVersion.SOFTWARE_BLOCK_13_14;
            }
        }


        private void FetchAdvertisements() {
            _advertisementsCollection = new ObservableCollection<Advertisement>(Advertisement.GetAllAdvertisements(_ddsConfiguration.ConnectionID));

            /*foreach (var ad in Advertisement.GetAllAdvertisements(_ddsConfiguration.ConnectionID)) {
                foreach (var topic in Enum.GetValues(typeof(CtvDdsCategories))) {
                    if (ad.Title.StartsWith(topic.ToString())) {
                        _advertisementsCollection.Add(ad);
                    }
                }
            }

            // TODO: I think we should filter by category not title, b/c there is NOT a standard the forces that advertisements have to start with any particular value.
            foreach (var ad in Advertisement.GetAllAdvertisements(_ddsConfiguration.ConnectionID)) {
                foreach (var ctvCategory in Enum.GetValues(typeof(CtvDdsCategories))) {
                    foreach (var category in ad.Categories) {
                        if (category == ctvCategory.ToString()) {
                            _advertisementsCollection.Add(ad);
                        }
                    }
                }
            }*/

        }

        public IEnumerable<Advertisement> GetAdvertisementsByCategory(string category) {
            var filtered = new List<Advertisement>();
            foreach (var advertisement in _advertisementsCollection) {
                foreach (var cats in advertisement.Categories) {
                    filtered.AddRange(from object ctvCat in Enum.GetValues(typeof (CtvDdsCategories)) where cats == ctvCat.ToString() select advertisement);
                }
            }
            return filtered;
        }


        public class CtvDdsCategories {
            private const string _sigAct = "SIG-ACT";
            private const string _enSit = "ENEMY-SIT";
            private const string _obsPos = "OBS-POS";
            private const string _graphics = "GRPAHICS";
            private const string _airTracks = "AIR-TRACKS";
            private const string _posRpt = "POS-RPT";

            public static string SIG_ACT {get { return _sigAct; }}
            public static string ENEMY_SIT {get { return _enSit; }}
            public static string OBS_POS {get { return _obsPos; }}
            public static string GRAPHICS {get { return _graphics; }}
            public static string AIR_TRACKS {get { return _airTracks; }}
            public static string POS_RPT {get { return _posRpt; }}
        };
    }
}
